/**
 * 
 */
package org.hotpotmaterial.codegenn2.config.bean;

import java.io.File;

import org.hotpotmaterial.codegenn2.common.HmLogConstants;
import org.slf4j.LoggerFactory;

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.encoder.PatternLayoutEncoder;
import ch.qos.logback.classic.filter.LevelFilter;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.rolling.RollingFileAppender;
import ch.qos.logback.core.rolling.TimeBasedRollingPolicy;
import ch.qos.logback.core.spi.FilterReply;

/**
 * @author cakydeveloper
 *
 */
public class LogConfig {

	private static String rootDir;

	private static String prefix;

	private static int maxhistory;

	/**
	 * @return the rootDir
	 */
	public static String getRootDir() {
		return rootDir;
	}

	/**
	 * @return the prefix
	 */
	public static String getPrefix() {
		return prefix;
	}

	/**
	 * @param rootDir the rootDir to set
	 */
	public void setRootDir(String rootDir) {
		LogConfig.rootDir = rootDir;
	}

	/**
	 * @param prefix the prefix to set
	 */
	public void setPrefix(String prefix) {
		LogConfig.prefix = prefix;
	}

	/**
	 * @return the maxhistory
	 */
	public int getMaxhistory() {
		return maxhistory;
	}

	/**
	 * @param maxhistory the maxhistory to set
	 */
	public void setMaxhistory(int maxhistory) {
		LogConfig.maxhistory = maxhistory;
	}

	public static Logger getLogger(String logname) {
		// 获取日志实例
		Logger logger = (Logger) LoggerFactory.getLogger(logname);
		if (null != logger.getAppender(logname.concat("-file-appender"))) {
			// 如果已经存在fileAppender则不需要再初始化
			return logger;
		}
		LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
		// file
		String logfilename = rootDir.concat(File.separator).concat(prefix).concat("-").concat(logname);
		// 创建RollingFileAppender
		RollingFileAppender<ILoggingEvent> fileAppender = new RollingFileAppender<ILoggingEvent>();
		// 设置名称
		fileAppender.setName(logname.concat("-file-appender"));
		fileAppender.setAppend(true);
		// 添加context
		fileAppender.setContext(lc);
		// 创建Encoder
		PatternLayoutEncoder ple = new PatternLayoutEncoder();
		ple.setPattern("%d{yyyy-MM-dd HH:mm:ss.SSS} %msg%n");
		ple.setContext(lc);
		ple.start();
		// 添加encoder
		fileAppender.setEncoder(ple);
		// 创建filter
		LevelFilter filter = new LevelFilter();
		if (HmLogConstants.EXEC_LOGNAME.equals(logname)) {
			filter.setLevel(Level.ERROR);
		} else {
			filter.setLevel(Level.INFO);
		}
		filter.setOnMatch(FilterReply.ACCEPT);
		filter.setOnMismatch(FilterReply.DENY);
		filter.start();
		// 添加filter
		fileAppender.addFilter(filter);
		// 创建RollingPolicy
		TimeBasedRollingPolicy<ILoggingEvent> policy = new TimeBasedRollingPolicy<>();
		policy.setFileNamePattern(logfilename.concat("-%d{yyyy-MM-dd}.log"));
		policy.setMaxHistory(maxhistory);
		policy.setParent(fileAppender);
		policy.setContext(lc);
		policy.start();
		// 添加RollingPolicy
		fileAppender.setRollingPolicy(policy);
		fileAppender.start();
		// 设置日志级别
		if (HmLogConstants.EXEC_LOGNAME.equals(logname)) {
			logger.setLevel(Level.ERROR);
		} else {
			logger.setLevel(Level.INFO);
		}
		logger.addAppender(fileAppender);

		Logger rootlogger = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
		rootlogger.detachAppender("console");

		return logger;
	}

}
